//
//  DailyThemesViewController.m
//  HLZhiHuDaily
//
//  Created by 胡林 on 16/3/1.
//  Copyright © 2016年 胡林. All rights reserved.
//每日主题

#import "DailyThemesViewController.h"
#import "DailyThemesViewModel.h"
#import "HomeTableViewCell.h"
#import "StoryContentViewModel.h"
#import "WKWebViewController.h"
@interface DailyThemesViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (weak,nonatomic)UITableView *mainTableView;
@property (weak,nonatomic)UIScrollView *mainScrollView;
@property (weak,nonatomic)UIImageView *imaView;
@property (weak,nonatomic)UIVisualEffectView *cover;

@property (strong,nonatomic)DailyThemesViewModel* viewmodel;
@end

@implementation DailyThemesViewController
//先判断点击的侧滑栏里哪一个选项
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initSubViews];
    _viewmodel = [[DailyThemesViewModel alloc] init];
    [_viewmodel addObserver:self forKeyPath:@"stories" options:NSKeyValueObservingOptionNew context:nil];
    [_viewmodel addObserver:self forKeyPath:@"imageURLStr" options:NSKeyValueObservingOptionNew context:nil];
    [_viewmodel addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
    [_viewmodel addObserver:self forKeyPath:@"editors" options:NSKeyValueObservingOptionNew context:nil];
    [_viewmodel getDailyThemesDataWithThemeID:self.themeID];

    // Do any additional setup after loading the view.
}
- (void)dealloc {
    [self.viewmodel removeObserver:self forKeyPath:@"stories"];
    [self.viewmodel removeObserver:self forKeyPath:@"imageURLStr"];
    [self.viewmodel removeObserver:self forKeyPath:@"name"];
    [self.viewmodel removeObserver:self forKeyPath:@"editors"];
}
//侧滑栏传值
-(void)initSubViews{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont boldSystemFontOfSize:20] ,NSForegroundColorAttributeName:[UIColor whiteColor]}];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    UIScrollView *sv = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    [self.view addSubview:sv];
    _mainScrollView = sv;
    
    UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, -24, kScreenWidth, 112)];
    iv.contentMode = UIViewContentModeScaleAspectFill;
    [_mainScrollView addSubview:iv];
    _imaView = iv;
    
    UITableView *tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight-64)];
    tv.dataSource = self;
    tv.delegate = self;
    tv.rowHeight = 88.f;
    tv.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tv];
    _mainTableView = tv;
    
    [_mainTableView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([HomeTableViewCell class])];
 
    UIBarButtonItem * leftBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"News_Arrow"] style:(UIBarButtonItemStylePlain) target:self action:@selector(back:)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    UIBarButtonItem *rightbarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Dark_Management_Add"] style:(UIBarButtonItemStylePlain) target:self action:@selector(attention:)];
     self.navigationItem.rightBarButtonItem = rightbarButton;
    
}
- (void)back:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)attention:(id)sender {
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    if ([keyPath isEqualToString:@"stories"]) {
        [_mainTableView reloadData];
    }
    if ([keyPath isEqualToString:@"imageURLStr"]) {
        [_imaView sd_setImageWithURL:[NSURL URLWithString:self.viewmodel.imageURLStr]];
    }
    if ([keyPath isEqualToString:@"name"]) {
        self.title = self.viewmodel.name;
    }
    
    if ([keyPath isEqualToString:@"editors"]) {
        if (self.viewmodel.editors.count > 0) {
            UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 44)];
            headerView.backgroundColor = [UIColor whiteColor];
            UILabel * lab = [[UILabel alloc] initWithFrame:CGRectMake(15, 7, 40, 30)];
            lab.text = @"主编";
            [headerView addSubview:lab];
            for (int i = 0; i < _viewmodel.editors.count; i++) {
                NSDictionary * dic = self.viewmodel.editors[i];
                UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(65+40 * i, 7, 30, 30)];
                [imageV sd_setImageWithURL:dic[@"avatar"]];
                imageV.layer.cornerRadius = 15;
                imageV.layer.masksToBounds = YES;
                [headerView addSubview:imageV];
            }
            self.mainTableView.tableHeaderView = headerView;
        }
    }
}
//指定每个分区中有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewmodel.stories.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:NSStringFromClass([HomeTableViewCell class])];
    cell.storyModel = self.viewmodel.stories[indexPath.row];
    return cell;
}

//选中Cell响应事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    StoryModel * model =self.viewmodel.stories[indexPath.row];
    StoryContentViewModel * scVM = [[StoryContentViewModel alloc] init];
    scVM.loadedStoryID = model.storyID;
    scVM.storiesID = [self.viewmodel.stories valueForKey:@"storyID"];
    scVM.storyType = model.type;
    self.navigationItem.backBarButtonItem = nil;
    [self.navigationController pushViewController:[[WKWebViewController alloc] initWithViewModel:scVM] animated:YES];
    
}
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end

